package com.kuang.test;


import java.util.concurrent.locks.ReentrantLock;

public class MyHeldThread {

    private ReentrantLock lock;

    public MyHeldThread(boolean fair){
        lock = new ReentrantLock(fair);
    }

    public void serviceMethod(){
        try{
            System.out.println(lock.isHeldByCurrentThread());
            lock.lock();
            System.out.println(lock.isHeldByCurrentThread());
        }finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        MyHeldThread myHeldThread = new MyHeldThread(true);
        new Thread(new Runnable() {
            @Override
            public void run() {
                myHeldThread.serviceMethod();
            }
        }).start();
    }
}
